import { Middleware } from '@midwayjs/core';
import * as cors from '@koa/cors';

@Middleware()
export class CorsMiddleware {
  resolve() {
    return cors({
      origin: (ctx) => {
        const allowedOrigins = ['http://localhost:3000'];
        const origin = ctx.get('Origin');
        if (allowedOrigins.includes(origin)) {
          return origin;
        }
        return false;
      },
      allowMethods: ['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH', 'OPTIONS'],
      allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
      exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
      maxAge: 5,
      credentials: true,
      keepHeadersOnError: true
    });
  }
} 